﻿//-----------------------------------------------------------------------
// <copyright file="ItemClickCommand.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport
{
    using System;
    using System.Windows.Input;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;

    /// <summary>
    /// Use this class for items that do not have triggers or command attributes built in to attach them.
    /// </summary>
    public class ItemClickCommand
    {
        /// <summary>
        /// Dependency property to be attached to items that need to have commanding for MVVM.
        /// </summary>
        public static readonly DependencyProperty AttachedCommandProperty = DependencyProperty.RegisterAttached(
            "AttachedCommand", typeof(ICommand), typeof(ItemClickCommand), new PropertyMetadata(null, AttachedCommandPropertyChanged));

        /// <summary>
        /// Sets the command
        /// </summary>
        /// <param name="attached">Dependency object to attach to.</param>
        /// <param name="value">Command to attach to the object.</param>
        public static void SetAttachedCommand(DependencyObject attached, ICommand value)
        {
            attached.SetValue(AttachedCommandProperty, value);
        }

        /// <summary>
        /// Returns the command attached to an object.
        /// </summary>
        /// <param name="attached">object you wish to find the command for.</param>
        /// <returns>The command attached to the object</returns>
        public static ICommand GetAttachedCommand(DependencyObject attached)
        {
            return (ICommand)attached.GetValue(AttachedCommandProperty);
        }

        /// <summary>
        /// creates an event handler for when a command is set.
        /// </summary>
        /// <param name="d">the dependency object</param>
        /// <param name="e">The parameter is not used.</param>
        private static void AttachedCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ListViewBase).ItemClick += ItemClickHandler;
        }

        /// <summary>
        /// Handler that is attached to the Item Click event.
        /// </summary>
        /// <param name="sender">This should be a ListViewBase object</param>
        /// <param name="e">events passed in as an argument.</param>
        private static void ItemClickHandler(object sender, ItemClickEventArgs e)
        {
            var viewBase = sender as ListViewBase;
            if (viewBase == null)
            {
                throw new Exception("Error in ItemClickHandler.  Sender is not of type ListViewBase.");
            }

            ICommand command = GetAttachedCommand(viewBase);
            command.Execute(e.ClickedItem);
        }
    }
}
